<template>
  <div class="imgs-upload">
    <el-upload
      :action="imgUploadUrl"
      list-type="picture-card"
      :headers="headers"
      multiple
      :file-list="fileList"
      :on-error="handleError"
      :before-upload="beforeUpload"
      :auto-upload="autoUpload"
      :on-change="handleChange"
      :limit="limit"
      :id="id"
    >
      <i class="el-icon-plus"></i>
      <div slot="file" slot-scope="{file}">
        <img v-if="showImg(file)" class="el-upload-list__item-thumbnail" :src="file.url" alt />
        <el-progress v-else type="circle" :percentage="parsePercentage(file.percentage)"></el-progress>
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-left" @click="move('left',file)">
            <i class="el-icon-arrow-left"></i>
          </span>
          <div class="center">
            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
              <i class="el-icon-zoom-in"></i>
            </span>
            <span class="el-upload-list__item-delete" @click="handleDownload(file)">
              <i class="el-icon-download"></i>
            </span>
            <span class="el-upload-list__item-delete" @click="handleRemove(file)">
              <i class="el-icon-delete"></i>
            </span>
          </div>
          <span class="el-upload-list__item-right" @click="move('right',file)">
            <i class="el-icon-arrow-right"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
import store from "@/store";
export default {
  props: {
    limit: {
      type: Number,
      default: 1,
    },
    id: {
      type: String,
      required: true,
    },
    files: {
      type: Array,
      required: true,
    },
    autoUpload: {
      type: Boolean,
      default: true,
    },
  },
  watch: {
    fileList() {
      // this.$emit("callBackFileList", this.fileList);
      if (this.fileList.length === this.limit) {
        document.getElementById(this.id).childNodes[1].style.visibility =
          "hidden";
        this.isAddNone = true;
      }
      if (this.fileList.length < this.limit && this.isAddNone) {
        document.getElementById(this.id).childNodes[1].style.visibility =
          "visible";
      }
    },
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + store.getters.token },
      imgUploadUrl:
        process.env.VUE_APP_BASE_API + '/file/upload',
      fileList: this.files,
      isAddNone: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
    };
  },
  methods: {
    beforeUpload(file) {
      let reader = new FileReader();
      reader.readAsDataURL(file); //这里把一个文件用base64编码,具体什么是base64编码,我将稍后在不久的文章中介绍
      reader.onload = function (e) {
        let img = new Image();
        img.src = e.target.result; //获取编码后的值,也可以用this.result获取

      };
    },
    showImg(file) {
      if (file.response && file.response.code === 200) {
        return true;
      }
      return false;
    },
    move(type, file) {
      let index = this.fileList.indexOf(file);
      if (type === "left") {
        let newFileList = [];
        let leftFile = this.fileList[index - 1];
        if (index > 0) {
          this.fileList.forEach((item) => {
            newFileList.push(item);
          });
          newFileList[index] = leftFile;
          newFileList[index - 1] = file;
          this.fileList = newFileList;
        }
      }
      if (type === "right") {
        let newFileList = [];
        let leftFile = this.fileList[index + 1];
        if (index < this.fileList.length - 1) {
          this.fileList.forEach((item) => {
            newFileList.push(item);
          });
          newFileList[index] = leftFile;
          newFileList[index + 1] = file;
          this.fileList = newFileList;
        }
      }
    },
    handleError() {
      this.$message({
        message: "上传失败",
        type: "error",
      });
    },
    parsePercentage(val) {
      return parseInt(val, 10);
    },
    handleChange(file, fileList) {
      if (file.response && file.response.code ) {
        if (file.response.code != 200){
        this.$message({
          message: "上传失败 " + file.response.msg,
          type: "error",
        });
        fileList.splice(this.fileList.indexOf(file), 1);
        }else{
          fileList[this.fileList.indexOf(file)].url = file.response.msg
        }
      }
      this.fileList = fileList;
    },
    handleRemove(file) {
      this.fileList.splice(this.fileList.indexOf(file), 1);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

  },
};
</script>

<style lang="scss" scoped>
.imgs-upload {
  .center {
    position: relative;
    top: 30%;
  }
  .el-upload-list__item-left {
    position: absolute;
    left: 10px;
    bottom: 10px;
  }
  .el-upload-list__item-right {
    position: absolute;
    right: 10px;
    bottom: 10px;
  }
}
</style>